<template>
    <div>
        <el-breadcrumb separator-class="el-icon-caret-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card>
            <el-row :gutter="30">
                <el-col :span="8">
                    <el-input placegolder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList(1)">
                        <el-button icon="el-icon-search" slot="append" @click="getGoodsList(1)"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addGoods">添加商品</el-button>
                </el-col>
            </el-row>


            <el-table :data="goodsList" stripe border>
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column label="商品名称" prop="goods_name"></el-table-column>
                <el-table-column label="商品的重量" prop="goods_weight" width="100px"></el-table-column>
                <el-table-column label="商品的价格(元)" prop="goods_price" width="120px"></el-table-column>
                <el-table-column label="创建时间" width="180px">
                    <template slot-scope="scope">
                        {{scope.row.add_time | dateFormat}}
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="130px">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="openEditDialog(scope.row.goods_id)"></el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" circle
                                   @click="deleteById(scope.row.goods_id)"></el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfo.pagenum"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="queryInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total" background>
            </el-pagination>

            <el-dialog
                    title="修改信息"
                    :visible.sync="setEditDialog"
                    width="50%"
                    >
                <el-form :model="goodsInfo" :rules="addFormRules" label-width="100px" ref="goodsInfoRef">
                    <el-form-item label="商品名称" prop="goods_name">
                        <el-input v-model="goodsInfo.goods_name"></el-input>
                    </el-form-item>
                    <el-form-item label="商品重量" prop="goods_weight">
                        <el-input type="number" v-model="goodsInfo.goods_weight"></el-input>
                    </el-form-item>
                    <el-form-item label="商品价格" prop="goods_price">
                        <el-input type="number" v-model="goodsInfo.goods_price"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="setEditDialog = false">取 消</el-button>
                    <el-button type="primary" @click="saveUpdate">确 定</el-button>
                </span>
            </el-dialog>

        </el-card>
    </div>
</template>

<script>
    export default {
        name: "Goods",
        data() {
            return {
                queryInfo: {
                    query: '',
                    pagenum: 1,
                    pagesize: 5
                },
                //商品列表数据
                goodsList: [],

                //商品的总数
                total: 0,

                //设置编辑模态框的显示
                setEditDialog: false,

                //回显商品的数据
                goodsInfo: {},

                addFormRules: {
                    goods_name: [
                        {required: true, message: "请输入商品名称", trigger: 'blur'}
                    ],
                    goods_price: [
                        {required: true, message: "请输入商品价格", trigger: 'blur'}
                    ],
                    goods_weight: [
                        {required: true, message: "请输入商品重量", trigger: 'blur'}
                    ]
                }
            }
        },

        created() {
            this.getGoodsList();
        },

        methods: {
            async getGoodsList(page = this.queryInfo.pagenum) {
                this.queryInfo.pagenum = page;
                const {data} = await this.$http.get(`goods`, {params: this.queryInfo});
                if (data.meta.status !== 200) return this.$message.error(data.msg);
                this.goodsList = data.data.goods;
                this.total = data.data.total;
            },

            //分页每页条数改变时
            handleSizeChange(newSize) {
                this.queryInfo.pagesize = newSize;
                this.getGoodsList();
            },

            //分页页码改变时
            handleCurrentChange(newPage) {
                this.queryInfo.pagenum = newPage;
                this.getGoodsList();
            },

            //根据商品的Id删除商品
            async deleteById(id) {
                const result = await this.$confirm('您确认要删除该商品嘛', '提示', {
                    type: 'error',
                    cancelButtonText: '取消',
                    confirmButtonText: '确认'
                }).catch(err => err);
                if (result === 'cancel') return this.$message.info('取消删除');
                const {data} = await this.$http.delete(`goods/${id}`);
                if (data.meta.status !== 200) return this.$message.error(data.meta.msg);
                this.getGoodsList();
                this.$message.success('删除成功');
            },

            //添加商品
            addGoods(){
                this.$router.push('/goods/add')
            },

            //打开商品编辑的模态框
            async openEditDialog(id){
                this.goodsInfo = {};
                const {data} = await this.$http.get(`goods/${id}`);
                if (data.meta.status !== 200) return this.$message.error(data.meta.msg);
                this.goodsInfo = data.data;
                this.setEditDialog = true;
            },

            //发送请求修改数据
            async saveUpdate(){
                const {data} = await this.$http.put(`goods/${this.goodsInfo.goods_id}`,this.goodsInfo);
                if (data.meta.status !== 200) return this.$message.error(data.meta.msg);
                this.getGoodsList();
                this.setEditDialog = false;
                this.$message.success('修改成功');
            }
        }
    }
</script>

<style scoped>

</style>
